package com.longge;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-06-09 23:21
 */
public class LengthOfLongestSubstring {

    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> window = new HashMap<>();
        char[] chars = s.toCharArray();
        int left = 0, right = 0, max = 0;
        while (right < chars.length) {
            char c = chars[right++];
            window.put(c, window.getOrDefault(c, 0) + 1);
            while (window.get(c) > 1) {
                char d = chars[left++];
                window.put(d, window.get(d) - 1);
            }
            max = Math.max(max, right - left);
        }
        return max;

    }
}
